﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace LiveMail
{
    public partial class TestForm : Form
    {
        private delegate void FormDelegation();
        private Thread thead;
        public TestForm()
        {
            InitializeComponent();
            cmbProtocol.SelectedIndex = 0;


            txtMailSubject.Text = Environment.UserName + DateTime.Now.ToString("yyyyMMdd-hh:mm:ss");
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                SmtpSendMail send = new SmtpSendMail(
                      txtSmtp.Text,
                      int.Parse(txtSmtpPort.Text),
                      txtUser.Text,
                      txtPass.Text);
                send.Send(txtMailSubject.Text, txtMailBody.Text, txtMailTo.Text);
                MessageBox.Show("mail was sent.");
            }
            finally
            {
                txtMailSubject.Text = "SMTP:" + Environment.UserName + DateTime.Now.ToString("yyyy-MMdd-hh:mm:ss");
            }
        }

        private void btnReceive_Click(object sender, EventArgs e)
        {

            FormDelegation del = new FormDelegation(
                () =>
                {
                    TestMailBase mail = null;
                    string url = string.Empty;
                    int port = 0;
                    if (cmbProtocol.SelectedIndex == 1)
                    {
                        url = txtPop.Text;
                        port = int.Parse(txtPopPort.Text);
                        mail = new PopMail(url, port, true);
                    }
                    else
                    {
                        url = txtImap.Text;
                        port = int.Parse(txtImapPort.Text);
                        mail = new InteractiveMail(url, port, true);
                    }
                    using (mail)
                    {
                        mail.OnServerMessage += mail_OnServerMessage;
                        lstSubjects.Items.Clear();

                        mail.Connect();
                        mail.AuthenticateUser(txtUser.Text, txtPass.Text);
                        var count = mail.ReadMailCount();
                        var top = count > 20 ? count - 10 : 1;

                        for (int i = count; i >= top; i--)
                        {
                            Application.DoEvents();
                            var content = mail.GetMail(i);
                            if (content.Count > 0)
                            {
                                string subject = i + "  " + content["SUBJECT"];
                                lstSubjects.Items.Add(subject);
                            }
                        }

                        mail.Disconnect();
                    }
                });

            thead = new Thread(
                () =>
                {
                    this.Invoke(del);
                });
            thead.Start();
        }

        void mail_OnServerMessage(object sender, EventArgs e)
        {
            if (sender != null)
            {
                txtLog.Text += sender.ToString() + Environment.NewLine;
            }
        }

    }
}
